package com.elongtian.uiactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.elongtian.adapter.DrawerAdapter;
import com.elongtian.adapter.MaijixieAdapter;
import com.elongtian.adapter.MyAdapter;
import com.elongtian.adapter.NewcarAdapter;
import com.elongtian.basic.http.HttpListener;
import com.elongtian.bbjx.R;
import com.elongtian.entity.Item;
import com.elongtian.model.Maijixie_beenInfo;
import com.elongtian.model.NewCarListBeenInfo;
import com.elongtian.model.SelectChoose_beenInfo;
import com.elongtian.model.getCity_BeenInfo;
import com.elongtian.model.getProvince_BeenInfo;
import com.elongtian.util.ConstantsUtil;
import com.elongtian.util.HttpUrl;
import com.elongtian.util.Loding.LoadingDialog;
import com.elongtian.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import limitpagereq.LimitedPager;
import limitpagereq.LimitedPagerConfig;
import limitpagereq.api.IRefreshView;
import limitpagereq.category.SimpleCategory;
import limitpagereq.impl.RefreshViewImlp;



public class SearchNewcarActivity extends BaseActivityTwo implements View.OnClickListener {

    private static final String GUOCHAN = "guochan";
    private TextView  tvArea,tv_title;
    private ListView listView;
    //private EditText tv_jixing;
    private DrawerLayout mDrawerLayout;
    private LinearLayout drawer_content, ll_newcar_pinpai, ll_newcar_area, ll_newcar_choose, ll_oldcar_choose;
    private LinearLayout ll_oldjixie_item, ll_newcar_shengchanguo;
    private RelativeLayout  rl_two, rl_three, rl_four, rl_seven, rl_eight;
    private TextView item_right_two, item_right_one, item_right_three, item_right_four, item_right_seven, item_right_eight;
    private TextView tv_cha;
    private LinearLayout ll_zong;
    private String shengchanguo = HttpUrl.shengchanguo;
    private String Leixing = HttpUrl.Leixing;
    private String Price = HttpUrl.home_prices;
    private String Pinpai = HttpUrl.Pinpai;
    private String Jixing = HttpUrl.Jixing;
    private String WorkHour = HttpUrl.WorkHour;
    private String Data = HttpUrl.Data;
    //旧机械筛选参数
    private TextView tv_oldcar_pinpai, tv_oldcar_leixing, tv_oldcar_scg;
    private RelativeLayout rl_gv, rl_listBackground;
    private TextView newcar_shengchanguo;
    private TextView tv_ok;
    private NewcarAdapter newcarAdapter;//出售
    private MaijixieAdapter maijixieAdapter;//求购

    private RadioGroup main_tab_group;
    private RadioButton search_find_newjx, search_find_oldjx;
    private ImageView ivBack, ivDrawerBack,ivChushou,ivGoumai;

    private List<NewCarListBeenInfo> newCarListBeenInfo = new ArrayList<>();
    private List<Maijixie_beenInfo> maijixieBeenInfo = new ArrayList<>();

    private List<SelectChoose_beenInfo> selectChoose_beenInfoList = new ArrayList<>();
    private List<getProvince_BeenInfo> provinceInfobeanlist = new ArrayList<>();
    private List<getCity_BeenInfo> cityInfobeanlist = new ArrayList<>();
    private List<String> list = new ArrayList<>();
    private ListView lv_drawerlist;
    private DrawerAdapter drawerAdapter;
    private SmartRefreshLayout smartRefreshLayout;
    private List<Item> items = new ArrayList<>();

    private com.elongtian.util.Loding.LoadingDialog dialog;
    private com.elongtian.util.Loding.LoadingDialog dialog01;


//    private NewcarSelcetAdapter nsAdapter;

    //    private ViewPager vpOrderContainer;
//    private TabLayout tabNavigation;
//    private List<Fragment> fragments;
    private GridView gv;//声明GridView视图
    private MyAdapter ma;
    private List<String> select = new ArrayList<>();
    private int clickStatus = 0;
    private int selct = 0;

    private String provimce;
    private String city;

    private int provimceId;
    private int cityId;
    private String quyuId;
    private String shengchanguoId;
    private String worktimeId;
    private String priceId;
    private String leixingId;
    private int pinpaiId;
    private String jixingId;
    private String xiaoshiId;
    private String chuchangId;
    private int selece = 0; //0 购买 1出售
    private int quyu = 0;
    private int tan = 0;//判断是测拉的还是弹框的
    private int type = 0;//是否是从首页进来的筛选传参

    //int pinpaiId=0;//从品牌页面传过来的id


    @Override
    protected String titleId() {
        return "新机械搜索";
    }

    @Override
    protected int contentViewId() {
        return R.layout.activity_search_newcar;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        RequestProvince();
        initData();
        initListener();


        /**
         * 接收首页的旧机械筛选参数
         */
        Intent fristIntent = getIntent();
        type = fristIntent.getIntExtra("type", 0);
        if (type == 1) {
            pinpaiId = fristIntent.getIntExtra("brandId", 0);
            provimceId = fristIntent.getIntExtra("provinceId", 0);
            priceId = fristIntent.getIntExtra("priceId", 0) + "";
            chuchangId = fristIntent.getIntExtra("dataId", 0) + "";
            search_find_oldjx.setChecked(true);
           // RequestOldcar();
            refresh();
            smartRefreshLayout.autoRefresh();
        } else {
           // RequestNewcar();
            refresh();
            smartRefreshLayout.autoRefresh();
        }

    }

    private void initData() {
        ll_oldjixie_item.setVisibility(View.VISIBLE);
        ll_newcar_shengchanguo.setVisibility(View.GONE);

        main_tab_group = (RadioGroup) findViewById(R.id.main_tab_group);
        main_tab_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.search_find_newjx:
                        selece = 0;
                        ll_oldjixie_item.setVisibility(View.VISIBLE);
                        ll_newcar_shengchanguo.setVisibility(View.GONE);
                        // TODO: 2017/12/26 买机械列表 
                        maijixieAdapter = new MaijixieAdapter(SearchNewcarActivity.this, maijixieBeenInfo);
                        listView.setAdapter(maijixieAdapter);
                        maijixieAdapter.notifyDataSetChanged();
                        clearData();
                        //RequestNewcar();
                        qiugourefresh();
                        smartRefreshLayout.autoRefresh();
                        break;
                    case R.id.search_find_oldjx:
                        selece = 1;
                        ll_oldjixie_item.setVisibility(View.VISIBLE);
                        ll_newcar_shengchanguo.setVisibility(View.GONE);
                        newcarAdapter = new NewcarAdapter(SearchNewcarActivity.this, newCarListBeenInfo);
                        listView.setAdapter(newcarAdapter);
                        newcarAdapter.notifyDataSetChanged();
                        clearData();
                        //RequestOldcar();
                        refresh();
                        smartRefreshLayout.autoRefresh();
                        break;
                }

            }
        });
        gv = (GridView) findViewById(R.id.gv);
        ma = new MyAdapter(SearchNewcarActivity.this, selectChoose_beenInfoList);
        gv.setAdapter(ma);//通过设置适配器实现网格内布局
        //为每个单元格（item）添加单击事件
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Button tv = (Button) view.findViewById(R.id.tv);
                Toast t = Toast.makeText(SearchNewcarActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT);
                t.setGravity(Gravity.TOP, 0, 0);
                t.show();
            }
        });


        newcarAdapter = new NewcarAdapter(SearchNewcarActivity.this, newCarListBeenInfo);
        listView.setAdapter(newcarAdapter);
        newcarAdapter.notifyDataSetChanged();

        drawerAdapter = new DrawerAdapter(SearchNewcarActivity.this, list);
        lv_drawerlist.setAdapter(drawerAdapter);
        lv_drawerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (selct == 5) {
                    provimce = list.get(i);
                    for (int j = 0; j < provinceInfobeanlist.size(); j++) {
                        if (provimce.equals(provinceInfobeanlist.get(j).getName())) {
                            RequestCity(provinceInfobeanlist.get(j).getId());
                        }
                    }
                    provimceId = provinceInfobeanlist.get(i).getId();
                    String yanan = provimceId + "";
                    Log.d("yananid", yanan);
                } else if (selct == 6) {
                    city = list.get(i);
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    item_right_three.setText(provimce + " " + city);
                    cityId = cityInfobeanlist.get(i).getId();
                    // Toast.makeText(SearchNewcarActivity.this, provimceId + " " + cityId, Toast.LENGTH_SHORT).show();
                    if (quyu == 1) {
                        if (selece == 0) {
                            //新机械
//                            RequestNewcar();
//                            refresh();
                            refresh();
                            smartRefreshLayout.autoRefresh();
                        } else if (selece == 1) {
                            //旧机械
//                            RequestOldcar();
//                            refresh();
                            refresh();
                            smartRefreshLayout.autoRefresh();
                        }
                        mDrawerLayout.closeDrawer(drawer_content);
                        quyu = 0;
                    }
                } else if (selct == 1) {
                    //生产国改为价格
                    priceId = selectChoose_beenInfoList.get(i).getId() + "";
                    item_right_seven.setText(list.get(i));
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();
                } else if (selct == 2) {
                    //品牌
                    pinpaiId = selectChoose_beenInfoList.get(i).getId();
                    item_right_two.setText(list.get(i));
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();

                } else if (selct == 3) {
                    //类型
                    leixingId = selectChoose_beenInfoList.get(i).getId() + "";
                    item_right_four.setText(list.get(i));
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();

                } else if (selct == 4) {
                    //机型
//                    jixingId = selectChoose_beenInfoList.get(i).getId() + "";
//                    item_right_five.setText(list.get(i));
//                    ll_zong.setVisibility(View.VISIBLE);
//                    tv_cha.setVisibility(View.VISIBLE);
//                    if (list.size() > 0) {
//                        list.clear();
//                    }
//                    drawerAdapter.notifyDataSetChanged();

                }  else if (selct == 8) {
                    //价格
                    priceId = selectChoose_beenInfoList.get(i).getId() + "";
                    item_right_seven.setText(list.get(i));
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();

                } else if (selct == 9) {
                    //出厂日期
                    chuchangId = selectChoose_beenInfoList.get(i).getId() + "";
                    item_right_eight.setText(list.get(i));
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();

                }
//                else if (selct == 10) {
//                    //出厂日期
//                    chuchangId = selectChoose_beenInfoList.get(i).getId() + "";
//                    item_right_eight.setText(list.get(i));
//                    ll_zong.setVisibility(View.VISIBLE);
//                    tv_cha.setVisibility(View.VISIBLE);
//                    if (list.size() > 0) {
//                        list.clear();
//                    }
//                    drawerAdapter.notifyDataSetChanged();
//
//                }
                else if (selct == 11) {
                    //类型
                    leixingId = selectChoose_beenInfoList.get(i).getId() + "";
                    mDrawerLayout.closeDrawer(drawer_content);
                    //RequestOldcar();
                    refresh();
                    smartRefreshLayout.autoRefresh();
                    if (list.size() > 0) {
                        list.clear();
                    }
                    drawerAdapter.notifyDataSetChanged();
                }


            }
        });

    }


    private void initView() {
        tv_title = (TextView) findViewById(R.id.header_title);
        tv_title.setText("机械买卖");
        ll_zong = (LinearLayout) findViewById(R.id.ll_zong);
        tv_cha = (TextView) findViewById(R.id.tv_cha);
        rl_two = (RelativeLayout) findViewById(R.id.rl_two);
        rl_three = (RelativeLayout) findViewById(R.id.rl_three);
        rl_four = (RelativeLayout) findViewById(R.id.rl_four);
        rl_seven = (RelativeLayout) findViewById(R.id.rl_seven);
        rl_eight = (RelativeLayout) findViewById(R.id.rl_eight);
        item_right_two = (TextView) findViewById(R.id.item_right_two);
        item_right_one = (TextView) findViewById(R.id.item_right);
        item_right_three = (TextView) findViewById(R.id.item_right_three);
        item_right_four = (TextView) findViewById(R.id.item_right_four);
        item_right_seven = (TextView) findViewById(R.id.item_right_seven);
        item_right_eight = (TextView) findViewById(R.id.item_right_eight);
        search_find_newjx = (RadioButton) findViewById(R.id.search_find_newjx);
        search_find_oldjx = (RadioButton) findViewById(R.id.search_find_oldjx);
        //tv_jixing = (EditText) findViewById(R.id.tv_jixing);

        ll_oldjixie_item = (LinearLayout) findViewById(R.id.ll_oldjixie_item);//旧机械参数布局
        ll_newcar_shengchanguo = (LinearLayout) findViewById(R.id.ll_newcar_shengchanguo);//新机械参数布局
        //搜索条件弹框布局
        rl_gv = (RelativeLayout) findViewById(R.id.rl_gv);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        //新机械参数（生产国，品牌，区域）
        newcar_shengchanguo = (TextView) findViewById(R.id.newcar_shengchanguo);
        tvArea = (TextView) findViewById(R.id.newcar_area);
        ll_newcar_pinpai = (LinearLayout) findViewById(R.id.ll_newcar_pinpai);
        ll_newcar_area = (LinearLayout) findViewById(R.id.ll_newcar_area);
        ll_newcar_choose = (LinearLayout) findViewById(R.id.newcar_choose);//新机械筛选


        //数据列表
        listView = (ListView) findViewById(R.id.lv_ll);
//侧滑控件
        lv_drawerlist = (ListView) findViewById(R.id.lv_drawerSelect);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        drawer_content = (LinearLayout) findViewById(R.id.drawer_content);
//旧机械参数（生产国，类型，品牌）
        tv_oldcar_pinpai = (TextView) findViewById(R.id.tv_oldcar_pinpai);
        tv_oldcar_leixing = (TextView) findViewById(R.id.tv_oldcar_leixing);
        tv_oldcar_scg = (TextView) findViewById(R.id.tv_oldcar_scg);
        ll_oldcar_choose = (LinearLayout) findViewById(R.id.oldcar_choose);

        ivBack = (ImageView) findViewById(R.id.header_left_but);
        ivDrawerBack = (ImageView) findViewById(R.id.iv_back_cela);
        ivChushou = (ImageView) findViewById(R.id.img_chushou);
        ivGoumai = (ImageView) findViewById(R.id.img_goumai);


        rl_listBackground = (RelativeLayout) findViewById(R.id.list_background);
        dialog = new LoadingDialog(SearchNewcarActivity.this, "加载中...");
        dialog01 = new LoadingDialog(SearchNewcarActivity.this, "加载中...");

        refresh();
//        if (search_find_newjx.isChecked()){
//            String newUrl="Mechanical/newsMechanicsList";
//            initRefresh();
//        }else if(search_find_oldjx.isChecked()){
//            String oldUrl="Mechanical/oldMechanicsList";
//            initRefresh();
//        }

    }

    String name;
    String newUrl = "";

    /**
     * 下拉刷新，上拉加载
     */
    public void refresh() {
        final Map<String, String> params = new HashMap<>();
        params.put("member_id", String.valueOf(SPUtils.getInstance(ConstantsUtil.SP_USERINFO).getInt(ConstantsUtil.SP_ID)));

            newUrl = "mechanical/mechanicalList";
            if (leixingId != null && !leixingId.equals("")) {
                params.put("mechanical_id", leixingId);
            }
            if (provimceId > 0) {
                params.put("province_id", String.valueOf(provimceId));
            }
            if (cityId > 0) {
                params.put("city_id", String.valueOf(cityId));
            }

            if (pinpaiId != 0) {
                params.put("brand_id", String.valueOf(pinpaiId));
                //CustomToast(pinpaiId + "");
            }
            if (chuchangId != null && !chuchangId.equals("")) {
                params.put("manufacture_date", chuchangId);
            }
            if (priceId != null && !priceId.equals("")) {
                params.put("price_id", priceId);
        }
        smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);

        final SimpleCategory<String> category2 = new SimpleCategory<String>("oldcar", "mechanical/mechanicalList") {
            @Override
            public void onRequestSuccessFinish(String result, boolean isRefresh) {
                if (smartRefreshLayout.isRefreshing()) {
                    smartRefreshLayout.finishRefresh();
                } else {
                    smartRefreshLayout.finishLoadmore();
                }

                newCarListBeenInfo = JSON.parseArray(result, NewCarListBeenInfo.class);
                if (search_find_newjx.isChecked()) {
                    if (isRefresh) {
                        maijixieAdapter.setData(maijixieBeenInfo);
                    } else {
                        maijixieAdapter.addData(maijixieBeenInfo);
                    }
                } else if (search_find_oldjx.isChecked()) {
                    if (isRefresh) {
                        newcarAdapter.setData(newCarListBeenInfo, 1);
                    } else {
                        newcarAdapter.addData(newCarListBeenInfo, 1);
                    }
                }
                newcarAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestFailureFinish(JSONObject result, boolean isRefresh) {
                super.onRequestFailureFinish(result, isRefresh);
            }

            @Override
            public Map<String, String> categoryParams() {
                return params;
            }

        };
        IRefreshView refreshView = new RefreshViewImlp(smartRefreshLayout);
        LimitedPagerConfig config = LimitedPager.newLimitedPagerConfigBuild().setBaseUrl(HttpUrl.UrlHead).add(category2).setRefreshView(refreshView);
        LimitedPager.getInstance().setConfig(config);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Log.e("sss", "initRefresh: ==========2222222==========");


                    Log.e("sss", "initRefresh: old");
                    name = "oldcar";
                    newUrl = "mechanical/mechanicalList";
                    Log.e("sss", "initRefresh: old" + category2.name());
                LimitedPager.getInstance().refreshByName(SearchNewcarActivity.this, name);
            }
        });

        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                Log.e("sss", "initRefresh: 333333333");

                    name = "oldcar";
                    newUrl = "mechanical/mechanicalList";

                LimitedPager.getInstance().loadmoreByName(SearchNewcarActivity.this, name);
            }
        });

    }


    public void qiugourefresh() {
        final Map<String, String> params = new HashMap<>();
        params.put("member_id", String.valueOf(SPUtils.getInstance(ConstantsUtil.SP_USERINFO).getInt(ConstantsUtil.SP_ID)));
            newUrl = "asktobuy/askToBuylistPage";
            if (leixingId != null && !leixingId.equals("")) {
                params.put("mechanical_id", leixingId);
            }
            if (provimceId > 0) {
                params.put("province_id", String.valueOf(provimceId));
            }
            if (cityId > 0) {
                params.put("city_id", String.valueOf(cityId));
            }

            if (pinpaiId != 0) {
                params.put("brand_id", String.valueOf(pinpaiId));
                //CustomToast(pinpaiId + "");
            }
            if (xiaoshiId != null && !xiaoshiId.equals("")) {
                params.put("work_hours", xiaoshiId);
            }
            if (chuchangId != null && !chuchangId.equals("")) {
                params.put("manufacture_date", chuchangId);
            }
            if (priceId != null && !priceId.equals("")) {
                params.put("price_id", priceId);
            }

        smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        final SimpleCategory<String> category = new SimpleCategory<String>(GUOCHAN, "asktobuy/askToBuylistPage") {
            @Override
            public void onRequestSuccessFinish(String result, boolean isRefresh) {
                if (smartRefreshLayout.isRefreshing()) {
                    smartRefreshLayout.finishRefresh();
                } else {
                    smartRefreshLayout.finishLoadmore();
                }
                maijixieBeenInfo = JSON.parseArray(result, Maijixie_beenInfo.class);
                if (search_find_newjx.isChecked()) {
                    if (isRefresh) {
                        maijixieAdapter.setData(maijixieBeenInfo);
                    } else {
                        maijixieAdapter.addData(maijixieBeenInfo);
                    }
                } else if (search_find_oldjx.isChecked()) {
                    if (isRefresh) {
                        newcarAdapter.setData(newCarListBeenInfo, 1);
                    } else {
                        newcarAdapter.addData(newCarListBeenInfo, 1);
                    }
                }
                maijixieAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestFailureFinish(JSONObject result, boolean isRefresh) {
                dialog01.close();
                super.onRequestFailureFinish(result, isRefresh);
            }

            @Override
            public Map<String, String> categoryParams() {
                return params;
            }
        };

        IRefreshView refreshView = new RefreshViewImlp(smartRefreshLayout);
        LimitedPagerConfig config = LimitedPager.newLimitedPagerConfigBuild().setBaseUrl(HttpUrl.UrlHead).add(category).setRefreshView(refreshView);
        LimitedPager.getInstance().setConfig(config);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Log.e("sss", "initRefresh: ==========2222222==========");


                    dialog01.show(SearchNewcarActivity.this);
                    Log.e("sss", "initRefresh: new");
                    newUrl = "asktobuy/askToBuylistPage";
                    name = GUOCHAN;
                    Log.e("sss", "initRefresh: new" + category.name());


                LimitedPager.getInstance().refreshByName(SearchNewcarActivity.this, name);
            }
        });

        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                Log.e("sss", "initRefresh: 333333333");

                    newUrl = "asktobuy/askToBuylistPage";
                    name = GUOCHAN;


                Log.e("sss", "initRefresh: 333333333" + category.name());
                LimitedPager.getInstance().loadmoreByName(SearchNewcarActivity.this, name);
            }
        });

    }

    private void initListener() {
        ll_newcar_pinpai.setOnClickListener(this);
        ll_newcar_area.setOnClickListener(this);
        search_find_newjx.setOnClickListener(this);
        search_find_oldjx.setOnClickListener(this);
        ll_newcar_choose.setOnClickListener(this);
        newcar_shengchanguo.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        rl_gv.setOnClickListener(this);
        tvArea.setOnClickListener(this);
        ivDrawerBack.setOnClickListener(this);
        ivChushou.setOnClickListener(this);
        ivGoumai.setOnClickListener(this);

        //旧机械监听
        tv_oldcar_pinpai.setOnClickListener(this);
        tv_oldcar_leixing.setOnClickListener(this);
        tv_oldcar_scg.setOnClickListener(this);
        ll_oldcar_choose.setOnClickListener(this);

        tv_ok.setOnClickListener(this);
        rl_two.setOnClickListener(this);
        rl_three.setOnClickListener(this);
        rl_four.setOnClickListener(this);
        rl_seven.setOnClickListener(this);
        rl_eight.setOnClickListener(this);
        tv_cha.setOnClickListener(this);
    }

    /**
     * 测拉返回
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (selct == 6) {
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                if (provinceInfobeanlist != null && provinceInfobeanlist.size() > 0) {
                    if (list.size() > 0) {
                        list.clear();
                    }
                    selct = 5;
                    for (int i = 0; i < provinceInfobeanlist.size(); i++) {
                        list.add(provinceInfobeanlist.get(i).getName());
                    }
                    drawerAdapter.notifyDataSetChanged();
                    mDrawerLayout.openDrawer(drawer_content);
                }
            } else if (selct == 1) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 2) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 3) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
            } else if (selct == 5) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 4) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 7) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 8) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (selct == 9) {
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                selct = 0;
            } else if (mDrawerLayout.isDrawerOpen(drawer_content)) {
                mDrawerLayout.closeDrawer(drawer_content);
            } else {
                return super.onKeyDown(keyCode, event);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }

    /**
     * 获取数据
     *
     * @param url
     */
    private void RequestSelect(String url) {
        dialog.show(SearchNewcarActivity.this);
        Request<JSONObject> requestGerCode = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
        request(SearchNewcarActivity.this, 0, requestGerCode, getSelectHttpListener, true, true);
        Log.d("getProvince", "=========================" + url.toString());
    }

    private HttpListener<JSONObject> getSelectHttpListener = new HttpListener<JSONObject>() {
        @Override
        public void onSucceed(int what, Response<JSONObject> response) {
            dialog.close();
            String result = String.valueOf(response.get());
            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray data = jsonObject.optJSONArray("data");
                Log.d("datayanan", data + "");
                if (jsonObject.optString("code").equals("0")) {
                    if (data != null && data.length() > 0) {
                        if (selectChoose_beenInfoList.size() > 0) {
                            selectChoose_beenInfoList.clear();
                        }
                        selectChoose_beenInfoList = JSON.parseArray(data.toString(), SelectChoose_beenInfo.class);
                        if (selectChoose_beenInfoList != null && selectChoose_beenInfoList.size() > 0) {
                            if (list.size() > 0) {
                                list.clear();
                            }

                            for (int i = 0; i < selectChoose_beenInfoList.size(); i++) {
                                if (selectChoose_beenInfoList.get(i).getName() != null && !selectChoose_beenInfoList.get(i).getName().equals("")) {
                                    list.add(selectChoose_beenInfoList.get(i).getName());
                                } else if (selectChoose_beenInfoList.get(i).getBrand_name() != null && !selectChoose_beenInfoList.get(i).getBrand_name().equals("")) {
                                    list.add(selectChoose_beenInfoList.get(i).getBrand_name());
                                } else if (selectChoose_beenInfoList.get(i).getModel_name() != null && !selectChoose_beenInfoList.get(i).getModel_name().equals("")) {
                                    list.add(selectChoose_beenInfoList.get(i).getModel_name());
                                } else if (selectChoose_beenInfoList.get(i).getMechanical_name() != null && !selectChoose_beenInfoList.get(i).getMechanical_name().equals("")) {
                                    list.add(selectChoose_beenInfoList.get(i).getMechanical_name());
                                }

                            }
                            ma.setData(selectChoose_beenInfoList);
                            ma.notifyDataSetChanged();
                            drawerAdapter.notifyDataSetChanged();
                            if (tan == 0) {
                                mDrawerLayout.openDrawer(drawer_content);
                            }

                        }
                    }
                } else {
                    Toast.makeText(SearchNewcarActivity.this, "服务出现问题", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                //LogUtils.e(e.toString());
            }
        }


        @Override
        public void onFailed(int what, Response<JSONObject> response) {
            if (dialog.isShow()) {
                dialog.close();
            }
        }
    };

    @Override
    public void onClick(View view) {
        String phone = SPUtils.getInstance(ConstantsUtil.SP_USERINFO).getString(ConstantsUtil.SP_PHONE);
        rl_gv.setVisibility(View.GONE);
        switch (view.getId()) {
            /**
             * 旧机械
             */
            case R.id.oldcar_choose:
                clearData();
                selece = 1;
                oldsaixuan();
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                mDrawerLayout.openDrawer(drawer_content);
                break;
            case R.id.tv_oldcar_pinpai:
                Intent intentOldcar = new Intent(SearchNewcarActivity.this, PickBrandsActivity.class);
                startActivityForResult(intentOldcar, ConstantsUtil.REQUEST_CODE_SEARCH_NEW_CAR_ACTIVITY);
                break;
            case R.id.tv_oldcar_leixing:
                clearData();
                ll_zong.setVisibility(View.GONE);
                mDrawerLayout.openDrawer(drawer_content);
                //lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(Leixing);
                selct = 11;
                break;
            case R.id.tv_oldcar_scg:
                if (select.size() > 0) {
                    select.clear();
                }
                RequestSelect(Price);
                ma.notifyDataSetChanged();
                tan = 1;
                rl_gv.setVisibility(View.VISIBLE);
                break;
            case R.id.newcar_choose:
                clearData();
                selece = 0;
                newsaixuan();
                ll_zong.setVisibility(View.VISIBLE);
                tv_cha.setVisibility(View.VISIBLE);
                rl_gv.setVisibility(View.GONE);
                mDrawerLayout.openDrawer(drawer_content);
                break;
            case R.id.newcar_shengchanguo:
                if (select.size() > 0) {
                    select.clear();
                }
                RequestSelect(shengchanguo);
                ma.notifyDataSetChanged();
                tan = 1;
                rl_gv.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_ok:
                priceId = ConstantsUtil.Select_ID + "";
                if (selece == 0) {
                    //新机械
                    //RequestNewcar();
                    refresh();
                    smartRefreshLayout.autoRefresh();
                } else if (selece == 1) {
                    //旧机械
                    //RequestOldcar();
                    refresh();
                    //refreshOldcar();
                    smartRefreshLayout.autoRefresh();
                }
                rl_gv.setVisibility(View.GONE);
                //RequestShengchanguoList();
                break;
            case R.id.ll_newcar_pinpai:
                //品牌
                Intent intentNewcar = new Intent(SearchNewcarActivity.this, PickBrandsActivity.class);
                startActivityForResult(intentNewcar, ConstantsUtil.REQUEST_CODE_SEARCH_NEW_CAR_ACTIVITY);
                break;
            case R.id.header_left_but:
                finish();
                break;
            case R.id.rl_gv:

                break;
            case R.id.iv_back_cela:
                if (selct == 6) {
                    ll_zong.setVisibility(View.GONE);
                    tv_cha.setVisibility(View.GONE);
                    lv_drawerlist.setVisibility(View.VISIBLE);
                    if (provinceInfobeanlist != null && provinceInfobeanlist.size() > 0) {
                        if (list.size() > 0) {
                            list.clear();
                        }
                        selct = 5;
                        for (int i = 0; i < provinceInfobeanlist.size(); i++) {
                            list.add(provinceInfobeanlist.get(i).getName());
                        }
                        drawerAdapter.notifyDataSetChanged();
                        mDrawerLayout.openDrawer(drawer_content);
                    }
                } else if (selct == 1) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 2) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 3) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                } else if (selct == 5) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 4) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 7) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 8) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else if (selct == 9) {
                    ll_zong.setVisibility(View.VISIBLE);
                    tv_cha.setVisibility(View.VISIBLE);
                    rl_gv.setVisibility(View.GONE);
                    selct = 0;
                } else {
                    mDrawerLayout.closeDrawer(drawer_content);
                }
                break;
            case R.id.img_chushou:
                if (TextUtils.isEmpty(phone)) {
                    startActivity(new Intent(SearchNewcarActivity.this, LoginActivity.class));
                    return;
                }
                Intent intentSubmitold = new Intent(SearchNewcarActivity.this, SubmitOldCar.class);
                startActivity(intentSubmitold);
                break;
            case R.id.img_goumai:
                if (TextUtils.isEmpty(phone)) {
                    startActivity(new Intent(SearchNewcarActivity.this, LoginActivity.class));
                    return;
                }
                Intent intent = new Intent(SearchNewcarActivity.this, SubmitNewCar.class);
                startActivity(intent);
                break;
            case R.id.rl_one:
                //生产国
                selct = 1;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                if (select.size() > 0) {
                    select.clear();
                }
                RequestSelect(shengchanguo);
                ma.notifyDataSetChanged();
                break;
            case R.id.rl_two:
                //品牌
                selct = 2;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(Pinpai);
                break;


            case R.id.newcar_area:
                clearData();
                quyu = 1;
            case R.id.rl_three:
                //区域
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                if (provinceInfobeanlist != null && provinceInfobeanlist.size() > 0) {
                    if (list.size() > 0) {
                        list.clear();
                    }
                    selct = 5;
                    for (int i = 0; i < provinceInfobeanlist.size(); i++) {
                        list.add(provinceInfobeanlist.get(i).getName());
                    }
                    drawerAdapter.notifyDataSetChanged();
                    mDrawerLayout.openDrawer(drawer_content);
                }
                break;
            case R.id.rl_four:
                //类型
                selct = 3;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(Leixing);
                break;

//            case R.id.rl_five:
//                //机型
//                selct = 4;
//                ll_zong.setVisibility(View.GONE);
//                tv_cha.setVisibility(View.GONE);
//                lv_drawerlist.setVisibility(View.VISIBLE);
//                RequestSelect(Jixing);
//                break;

            case R.id.rl_six:
                //工作时间
                selct = 7;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(WorkHour);
                break;
            //价格
            case R.id.rl_seven:
                cleartanData();
                selct = 8;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(Price);
                break;
            //出厂日期
            case R.id.rl_eight:

                cleartanData();
                selct = 9;
                ll_zong.setVisibility(View.GONE);
                tv_cha.setVisibility(View.GONE);
                lv_drawerlist.setVisibility(View.VISIBLE);
                RequestSelect(Data);
                break;
            case R.id.tv_cha:
                if (selece == 0) {
                    //新机械
                    //RequestNewcar();
                    refresh();
                    smartRefreshLayout.autoRefresh();
                } else if (selece == 1) {
                    //旧机械
                    //RequestOldcar();
                    refresh();
                    smartRefreshLayout.autoRefresh();
                }
                mDrawerLayout.closeDrawer(drawer_content);
                break;
        }
    }


    private void newsaixuan() {
        rl_two.setVisibility(View.VISIBLE);
        rl_three.setVisibility(View.VISIBLE);
        rl_four.setVisibility(View.GONE);
        rl_seven.setVisibility(View.GONE);
        rl_eight.setVisibility(View.GONE);
    }

    private void oldsaixuan() {
        rl_two.setVisibility(View.VISIBLE);
        rl_three.setVisibility(View.VISIBLE);
        rl_four.setVisibility(View.VISIBLE);
        rl_seven.setVisibility(View.VISIBLE);
        rl_eight.setVisibility(View.VISIBLE);
    }

    private void clearData() {
        provimceId = 0;
        cityId = 0;
        quyuId = "";
        leixingId = "";
        shengchanguoId = "";
        worktimeId = "";
        priceId = "";
        pinpaiId = 0;
        jixingId = "";
        xiaoshiId = "";
        chuchangId = "";
        item_right_two.setText("");
        item_right_three.setText("");
        item_right_four.setText("");
        item_right_seven.setText("");
        item_right_eight.setText("");

    }

    private void cleartanData() {
        if (list.size() > 0) {
            list.clear();
        }
        drawerAdapter.setData(list);
        drawerAdapter.notifyDataSetChanged();
    }


    public void RequestProvince() {
        String url = HttpUrl.getProvince;
        Request<JSONObject> requestGerCode = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
        request(SearchNewcarActivity.this, 0, requestGerCode, getProvinceHttpListener, true, true);
        Log.d("getProvince", "=========================" + url.toString());
    }

    private HttpListener<JSONObject> getProvinceHttpListener = new HttpListener<JSONObject>() {
        @Override
        public void onSucceed(int what, Response<JSONObject> response) {

            String result = String.valueOf(response.get());

            try {
                JSONObject jsonObject = new JSONObject(result);
                String province = jsonObject.optString("name");
                String id = jsonObject.optString("id");
                JSONArray data = jsonObject.optJSONArray("data");

                if (data != null && data.length() > 0) {
                    provinceInfobeanlist = JSON.parseArray(data.toString(), getProvince_BeenInfo.class);

                } else {
                    Toast.makeText(SearchNewcarActivity.this, "服务出现问题", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                //LogUtils.e(e.toString());
            }
        }


        @Override
        public void onFailed(int what, Response<JSONObject> response) {
//            mRefreshLayout.endRefreshing();
//            mRefreshLayout.endLoadingMore();
        }
    };

    public void RequestCity(int id) {
        String url = HttpUrl.getCity;
        Request<JSONObject> requestGerCode = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
        requestGerCode.add("parent", id);
        request(SearchNewcarActivity.this, 0, requestGerCode, getCityHttpListener, true, true);
        Log.d("getProvince", "=========================" + url.toString());
    }

    private HttpListener<JSONObject> getCityHttpListener = new HttpListener<JSONObject>() {
        @Override
        public void onSucceed(int what, Response<JSONObject> response) {

            String result = String.valueOf(response.get());

            try {
                JSONObject jsonObject = new JSONObject(result);
                // city = jsonObject.optString("name");
                JSONArray data = jsonObject.optJSONArray("data");

                if (data != null && data.length() > 0) {
                    cityInfobeanlist = JSON.parseArray(data.toString(), getCity_BeenInfo.class);
                    if (cityInfobeanlist != null && cityInfobeanlist.size() > 0) {
                        if (list.size() > 0) {
                            list.clear();
                        }
                        selct = 6;
                        for (int i = 0; i < cityInfobeanlist.size(); i++) {
                            list.add(cityInfobeanlist.get(i).getName());
                        }
                        drawerAdapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(SearchNewcarActivity.this, "服务出现问题", Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
                //LogUtils.e(e.toString());
            }

        }


        @Override
        public void onFailed(int what, Response<JSONObject> response) {
//            mRefreshLayout.endRefreshing();
//            mRefreshLayout.endLoadingMore();
        }
    };


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ConstantsUtil.REQUEST_CODE_SEARCH_NEW_CAR_ACTIVITY:
                if (data == null) return;
                pinpaiId = data.getIntExtra("brands_id", -1);
                Log.d("brands_id", pinpaiId + "");
                if (pinpaiId != -1) {
                    // TODO: 2017/10/18 做筛选功能
                    if (selece == 0) {
                        refresh();
                        smartRefreshLayout.autoRefresh();
                    } else if (selece == 1) {
                        refresh();
                        smartRefreshLayout.autoRefresh();
                    }

                }
                break;
        }
    }

}
